﻿using System.Xml;

namespace _44_C_怎样处理xml文件中的注释
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = @"..\..\..\Student.xml";//当前目录的上一级目录 再上级目录 再上级目录的 Student.xml

            XmlDocument doc = new XmlDocument();
            //doc.Load(path);

            XmlReaderSettings setting = new XmlReaderSettings();
            setting.IgnoreComments = true;//忽略注释
            XmlReader xmlread=XmlReader.Create(path, setting);
            doc.Load(xmlread);

            XmlNode xmlNode = doc.SelectSingleNode("StudentInfo");
            XmlNodeList xmlNodeList = xmlNode.ChildNodes;//字节点
            List<Student> list = new List<Student>();

            foreach (XmlNode item in xmlNodeList)
            {
                //if (item is XmlComment)//检查是不是注释
                //{
                //    Console.WriteLine(((XmlComment)item).InnerText);
                //    continue;
                //}
                Student student = new Student();

                XmlElement xmlElement = (XmlElement)item;
                student.Grade = xmlElement.GetAttribute("Grade");
                student.Teacher = xmlElement.GetAttribute("Teacher");

                XmlNodeList xmlNodeList1 = xmlElement.ChildNodes;
                student.Name = xmlNodeList1.Item(0).InnerText;
                student.age = Convert.ToInt32(xmlNodeList1.Item(1).InnerText);

                list.Add(student);
            }

            list.ForEach(student => Console.WriteLine($"{student.Name}  {student.age}"));

            Console.ReadKey();
        }
    }
}
